<?php
class bbs
{
	private $user_content;
	private $user_id;
	private $sql_add;
	private $sql_commit;
	private $current_time;
	private $hasNum;
	private $hasQuery;
	private $sql_update;
	private $sql_list;
	private $sql_id;
	private $query_id;
	private $row_id;
	private $query_list;
	private $row_list;
	private $sql_delete;

	public function __construct($user_mail)
	{	
		
		include('common/config.php');
	
		new connectDb();
		
		$this->getUserId($user_mail);

		$this->currentTime();
	}

	private function insertContent()
	{
		$this->sql_add = "insert into food_bbs (`user_id`,`content`) values ($this->user_id,'$this->user_content')";
		$this->query($this->sql_add);
	}

	private function updateContent()
	{
		$this->sql_update = "update food_bbs set content = '$this->user_content' where user_id = $this->user_id and create_time like '$this->current_time%' limit 1";
		$this->query($this->sql_update);
	}
 
	private function hasCommit()
	{	
		$sql_commit = "select id from food_bbs where user_id = $this->user_id and create_time like '$this->current_time%'";
		
		$this->hasQuery = $this->query($sql_commit);		
	 	$this->hasNum = $this->num($this->hasQuery);	
	}					
	
	public function handContent($user_content)
	{
		$this->user_content = $this->in($user_content);
		$this->hasCommit();

		if($this->hasNum > 0)
		{
			$this->updateContent();
		}else
		{
			$this->insertContent();
		}
	}

	public function deleteContent($bbs_id)
	{
		$this->sql_delete = "delete from food_bbs where id = '$bbs_id' order by id asc limit 1";
		$this->query($sql->sql_delete);
	}

	private function getUserId($user_mail)
	{
	 	$this->sql_id = "select id from food_user_basic where user_mail = '$user_mail'";
		$this->query_id = $this->query($this->sql_id);
		
		$this->row_id = $this->fetch_assoc($this->query_id);

 		$this->user_id =  $this->row_id['id'];

	}

	private function currentTime()
	{
		 $this->current_time = date("Y-m-d");
	}

	private function query($sql)
	{
		$query =  mysql_query($sql) or die(mysql_error());

		return $query;
	}

	private function fetch_assoc($query)
	{
		return mysql_fetch_array($query);
	}
	
	private function num($query)
	{
		return mysql_num_rows($query);
	}

	private function in($data,$force=false)
	{
		if(is_string($data))
		{
			$data=trim(htmlspecialchars($data));//防止被挂马，跨站攻击
			if(($force==true)||(!get_magic_quotes_gpc())) 
			{
		   	$data = addslashes($data);//防止sql注入
			}
			return  $data;
		}
		else if(is_array($data))//如果是数组采用递归过滤
		{
			foreach($data as $key=>$value)
			{	
				 $data[$key]=in($value);
			}
			return $data;
		}
		else 
		{
			return $data;
		}	
	}

	private function out($data)
	{
		if(is_string($data))
		{
			return $data = stripslashes($data);
		}
		else if(is_array($data))//如果是数组采用递归过滤
		{
			foreach($data as $key=>$value)
			{
				 $data[$key]=out($value);
			}	
			return $data;
		}
		else 
		{
			return $data;
		}	
	}


	}//end class

